// pages/des/des.js
import {
  GET_CHACK_DES,
  GET_CLASS_TREE,
  ADD_INSPECT_RESULT,
  GET_INSPECT_RESULT,
  UPLOAD_FILE,
  FILE_DEL
} from '../../request/api'
import { previewUrl } from '../../request/http'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    curId: null,
    info: {},
    treeData: [],
    show: false,
    content: '',
    radio: {},
    contents: {},
    uploadFileUrl: {},
    curIndexCode: null,
    fileList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      curId: options.id
    })
    this.getData(options.id, () => {
      // this.getTree()
      this.getResData()
    })
  },

  getData(id, cb) {
    GET_CHACK_DES(id).then(res => {
      this.setData({
        info: res
      })
      cb()
    })
  },

  getTree() {
    let type_map = {
      1: 4,
      2: 5
    }
    GET_CLASS_TREE({
      type: type_map[this.data.info.type] || 4
    }).then(res => {
      this.setData({
        treeData: res
      })
    })
  },

  showPop(e) {
    let fileStr = this.data.uploadFileUrl[e.currentTarget.dataset.code] || ''
    let fileArr = []
    let arr = []
    if(fileStr != ''){
      if(fileStr.indexOf(',') > 0){
        fileArr = fileStr.split(',')
      }else{
        fileArr = [ fileStr ]
      }
    }
    for(let i in fileArr){
      arr.push({
        url: `${previewUrl}${fileArr[i]}`,
        name: fileArr[i],
        fileUrl: fileArr[i],
        id: e.currentTarget.dataset.code
      })
    }
    this.setData({
      curIndexCode: e.currentTarget.dataset.code,
      show: true,
      content: this.data.contents[e.currentTarget.dataset.code] || '',
      fileList: arr
    })
  },

  onClose() {
    this.setData({
      show: false
    })
  },

  onChange(e) {
    let code = e.currentTarget.dataset.code;
    let detail = e.detail
    let data = `radio.${code}`
    this.setData({
      [data]: detail
    })
  },

  treeForeach(tree, func) {
    let node, list = [...tree]
    while (node = list.shift()) {
      func(node)
      node.subList && list.push(...node.subList)
    }
  },

  saveContent(e) {
    let data = `contents.${this.data.curIndexCode}`
    let fileData = `uploadFileUrl.${this.data.curIndexCode}`
    let fileUrlArr = []
    let files = this.data.fileList
    for(let i in files){
      fileUrlArr.push(files[i].fileUrl)
    }
    this.setData({
      [data]: this.data.content,
      [fileData]: fileUrlArr.toString(),
      show: false,
      fileList: []
    })
  },

  //上传照片
  afterRead(event) {
    let data = this.data.curIndexCode
    const { file } = event.detail
    UPLOAD_FILE(file.url).then(res => {
      let arr = this.data.fileList
      arr.push({
        url: `${previewUrl}${res.fileUrl}`,
        name: res.fileName,
        fileUrl: res.fileUrl,
        id: data
      })
      this.setData({
        fileList: arr
      })
    })
  },

  //删除照片
  afterReadIdBackDelete(e){
    let { file, index } = e.detail
    FILE_DEL({ path: file.fileUrl }).then(res => {
      let arr = this.data.fileList;
      arr.splice(index, 1)
      this.setData({
        fileList: arr
      })
    })
  },

  getResData() {
    GET_INSPECT_RESULT({ id: this.data.info.id }).then(res => {
      this.setData({
        treeData: res
      })
      this.treeForeach(res, (node) => {
        if (node.checkResult) {
          let data = `contents.${node.code}`
          let rdata = `radio.${node.code}`
          let fdata = `uploadFileUrl.${node.code}`
          this.setData({
            [data]: node.checkResult.reason || '',
            [rdata]: node.checkResult.status || '',
            [fdata]: node.checkResult.afterAnnex || '',
            show: false
          })
        }
      })
    })
  },

  submit() {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '请确认填报内容，提交后不可修改，是否提交？',
      success(res) {
        if (res.confirm) {
          let temp = []
          for (let i in that.data.radio) {
            temp.push({
              inspectCode: that.data.info.id,
              status: that.data.radio[i],
              reason: that.data.contents[i] || '',
              afterAnnex: that.data.uploadFileUrl[i] || '',
              sortCode: i
            })
          }
          ADD_INSPECT_RESULT({
            data: temp
          }).then(res => {
            wx.showToast({
              title: '提交成功',
              icon: 'success',
              duration: 2000
            })
          })
        }
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },
})